package com.unf.edu.rms.service;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.unf.edu.rms.dao.Faculty;
import com.unf.edu.rms.dao.Student;

@Service
public class StudentService {

	@Autowired
	private SessionFactory sessionFactory;
	

	@Transactional
	public List<Student> viewStudentTable()
	{
		System.out.println("StudentService.viewStudentTable()");
		Session session = sessionFactory.getCurrentSession();
		String hql = "FROM Student";
		Query query = session.createQuery(hql);
		List <Student> studentList = query.list();
		for(int i = 0; i < studentList.size(); i++){
			Student student = (Student) studentList.get(i);
			System.out.println("StudentService.viewStudentTable()====="+student.getFirstName());
			System.out.println("StudentService.viewStudentTable()====="+student.getLastName());
			System.out.println("StudentService.viewStudentTable()====="+student.getNnumber());
		}
		
		return studentList;
	}
	
	@Transactional
	public void addNewStudent(Student student)
	{
		Session session = sessionFactory.getCurrentSession();
		session.save(student);
	}
	
	@Transactional
	public Student getStudentById(int studentId)
	{
		Session session = sessionFactory.getCurrentSession();
		String hql = "FROM Student where studentId=:studentId";
		Query query = session.createQuery(hql);
		query.setInteger("studentId", studentId);
		Student student = (Student)query.list().get(0);
		return student;
	}
	
	@Transactional
	public void updateStudent(Student student)
	{
		Session session = sessionFactory.getCurrentSession();
		String hql = "FROM Student where studentId=:studentId";
		Query query = session.createQuery(hql);
		query.setInteger("studentId", student.getStudentId());
		session.update(student);
	}
}
